/** path to config-font file */
var urlFont = document.getElementById("txtRequest").value
		+ "/Config/config-font.xml";
/** array save font name */
var fontFamilyName = [];
/** array save font alias */
var fontFamilyAlias = [];
/** boolean check load file */
var bLoad = false;
/** select feature control */
var ctrlSelectFeature = null;
/** object read */
var objRead = null;
/** array save field */
var arrField = [];
var arrFieldAction=[]; 
/** array save alias field */
var arrAlias = [];
var fieldName=null;
var fieldAlias=null;
var layerActivate=null;
var divSelect=base.getElementById("div_on_off");
var divSelectLayer=base.getElementById("select_layer_tool");
var divSelectTooltip=base.getElementById("div_fill_select_tooltip");
var divSelectLabel = base.getElementById("div_fill_select_label");
/** portlet namespace value */
var nameSpace = document.getElementById("hiddenNamespace").value;

var index=0;
function read() {
	if (!bLoad) {
		objRead = setTimeout("read();", 1000);
	} else {
		clearTimeout(objRead);
	}
}
function openLabelTooltip(){
	/*CurrentFunction.close();
	CurrentFunction.name = Fuctions.labelTooltip;*/
	if(selectToolSelectLayer!=null)
		divSelectLayer.style.display="block";
	else{
		initArray();
		loadAttribute();
		initFont();
		formSelectTooltip();
		selectLayer();
	}
}
function selectLayer(){	
	if(selectToolSelectLayer!=null)
		divSelectLayer.style.display="block";
	else{
		divSelect.style.display="block";
	}
}

function getLayerSelect(){
	var index=selectToolSelectLayer[selectToolSelectLayer.selectedIndex].value;
	if(index==0)
		divSelect.style.display="none";
	else
	{
		divSelect.style.display="block";
		initArray();
		loadAttribute();
		initFont();
		formSelectTooltip();
	}
	var objChk=document.getElementById(nameSpace+"_show_hidden_label");
	objChk.checked=false;
	var objTooltip=document.getElementById(nameSpace+"_show_hidden_tooltip");
	objTooltip.checked=false;
}
/**
 * 		load font family 
 */
function loadFont() {
	/** number i initial value is 0 */
	var i = 0;
	/** number j initial value is 0 */
	var j = 0;
	/** font name */
	var fname = null;
	/** font alias */
	var falias = null;
	/** jquery read xml file */
	$.get(urlFont, function(data) {
		/** find font tag root */
		$(data).find('font').each(function() {
			var $field = $(this);
			/** find FontFamily element */
			$field.find('FontFamily').each(function() {
				var family = $(this);
				/** read Name attribute */
				fname = family.attr('Name');
				/** read Alias attribute */
				falias = family.attr('Alias');
				/** add family name in font family name array */
				fontFamilyName[i++] = fname;
				/** add family alias in font family alias array */
				fontFamilyAlias[j++] = falias;
			});
		});
	});
}
/**
 * 		show tooltip
 * 	@param 	checkbox	checkbox control
 */
function showTooltip(checkbox){
	var indexLayer = 0;
	if(selectToolSelectLayer==null){
		for(var n=0;n<vectorArray.length;n++){
			if(vectorArray[n].LayerId==layerParam){
				indexLayer=vectorArray[n].IndexLayer;
				break;
			}
		}
		formSelectTooltip();
	}else
		indexLayer = selectToolSelectLayer[selectToolSelectLayer.selectedIndex].value;
	index = indexLayer;
	/** layer select */
	layerActivate=map.layers[indexLayer];
	if (checkbox.checked) {
		divSelectTooltip.style.display="block";
	}else{
		divSelectTooltip.style.display="none";
		closeTooltip();
	}
}
/**
 * 		set tooltip feature
 * @param layerSelect	Layer select
 */
function setTooltipFeature(layerSelect){
	if(ctrlSelectFeature != null) 
	{
		map.removeControl(ctrlSelectFeature);
		ctrlSelectFeature = null;
	}
	/** defind control select feature is control in Openlayer */
	ctrlSelectFeature = new OpenLayers.Control.SelectFeature(layerSelect, {
		hover : true,
		onSelect : onSelectFeature,
		onUnselect : onUnSelectFeature,		
	});
	/** map add control select feature */
	map.addControl(ctrlSelectFeature);
	/** activate control select feature */
	ctrlSelectFeature.activate();	
}
/**
 * 		mouse move feature
 * @param feature	feature select
 */
function onSelectFeature(feature) {
	/** set select is feature */
	select = feature;
	/** string show */
	var strShow = '';
	/** value of feature */
	var fValue = null;
	var indexLayer=null;
	if(selectToolCboLayers==null){
		for ( var i = 0; i < vectorArray.length; i++) {
			if(vectorArray[i].LayerId==layerParam){
				indexLayer=vectorArray[i].IndexLayer;
				break;
			}
		}
	}else
		indexLayer=selectToolCboLayers[selectToolCboLayers.selectedIndex].value;
	var layerID=vectorArray[indexLayer-numBaseLayer].LayerId;
	/** set value for fValue and string show */
	for ( var i = 0; i < vectorArray.length; i++) {
		if (vectorArray[i].LayerId == layerID) {
			fValue = feature.attributes[fieldName];
			if (fValue == undefined) {
				fValue = "";
			}
			strShow += fieldAlias + ": " + fValue;
			break;
		}
	}
	/** defind object popup is popup frame cloud in Openlayer */
	objPopup = new OpenLayers.Popup.FramedCloud("HoverPop", feature.geometry
			.getBounds().getCenterLonLat(), null,
			"<div style='font-size:.9em;'>" + strShow + "</div>", null, false,
			onPopupClose);
	/** set size for popup */
	objPopup.autoSize = true;
	feature.popup = objPopup;
	/** add popup in map */
	map.addPopup(objPopup);
}
/**
 * 		 mouse out feature
 * 	@param feature	feature out hover
 */
function onUnSelectFeature(feature) {
	/** remove popup on map */
	map.removePopup(feature.popup);
	/** destroy popup */
	feature.popup.destroy();
}
/**
 * 		 close popup when mouse out feature
 *	 @param evt Event mouse out feature
 */
function onPopupClose(evt) {
	/** control select feature unselect feature */
	ctrlSelectFeature.unselect(select);
	/** set feature null */
	select = null;
}
/**
 * 		 close tooltip when mouse out feature
 */
function closeTooltip() {
	/** deactivate control select feature */
	ctrlSelectFeature.deactivate();
}

function loadAssetAttribute(){
	var indexLayer = 0;
	initArray();
	if(selectToolCboLayers==null){
		for(var n=0;n<vectorArray.length;n++){
			if(vectorArray[n].LayerId==layerParam){
				indexLayer=vectorArray[n].IndexLayer;
				break;
			}
		}
	}
	else
		indexLayer = selectToolCboLayers[selectToolCboLayers.selectedIndex].value;
	var layerID = vectorArray[indexLayer - numBaseLayer].LayerId;
	var i=0;
	if(arrFieldAction.length==0){
		for (i = 0; i < arrFieldAlias.length; i++) {
			if (base.trim(arrFieldAlias[i].LayerName.toLowerCase()) == base.trim(layerID.toLowerCase())) {
				arrFieldAction[arrFieldAction.length] = arrFieldAlias[i];
			}
		}
		arrFieldAction.sort(function(a, b) {
			var x = a.Index - b.Index;
			return x;
		});
	}
	for(i=0;i<arrFieldAction.length;i++){
		if(arrFieldAction[i].IsShowLabel){
			arrField[arrField.length]=arrFieldAction[i].Name;
			arrAlias[arrAlias.length]=arrFieldAction[i].Alias;
		}
	}
}

function loadAttribute(){
	var indexLayer = 0;
	if(selectToolSelectLayer==null){
		for(var n=0;n<vectorArray.length;n++){
			if(vectorArray[n].LayerId==layerParam){
				indexLayer=vectorArray[n].IndexLayer;
				break;
			}
		}
	}else
		indexLayer = selectToolSelectLayer[selectToolSelectLayer.selectedIndex].value;
	/** id of layer select */
	var layerID = vectorArray[indexLayer - numBaseLayer].LayerId;
	var i=0;
	if(arrFieldAction.length==0){
		for (i = 0; i < arrField_Alias.length; i++) {
			if (base.trim(arrField_Alias[i].LayerName.toLowerCase()) == base.trim(layerID.toLowerCase())) {
				arrFieldAction[arrFieldAction.length] = arrField_Alias[i];
			}
		}
		arrFieldAction.sort(function(a, b) {
			var x = a.Index - b.Index;
			return x;
		});
	}
	for(i=0;i<arrFieldAction.length;i++){
		if(arrFieldAction[i].IsShowLabel){
			arrField[arrField.length]=arrFieldAction[i].Name;
			arrAlias[arrAlias.length]=arrFieldAction[i].Alias;
		}
	}
}

function initArray(){
	arrField=new Array();
	arrFieldAction=new Array();
	arrAlias=new Array();
}

function initFont(){
	/** set varible boolean load is false */
	bLoad = false;
	/** initial array save font alias is null */
	fontFamilyAlias = new Array();
	/** initial array save font name is null */
	fontFamilyName = new Array();
	/** call function load font */
	loadFont();
	/** call function read */
	read();
}

function getStyleFromCookie(){
	var indexLayer = 0;
	if(selectToolSelectLayer==null){
		for(var n=0;n<vectorArray.length;n++){
			if(vectorArray[n].LayerId==layerParam){
				indexLayer=vectorArray[n].IndexLayer;
				break;
			}
		}
	}else
		indexLayer = selectToolSelectLayer[selectToolSelectLayer.selectedIndex].value;
	var sLayerIdSelect = vectorArray[indexLayer - numBaseLayer].LayerId;
	var objStyle = Cookies.getCookie(sLayerIdSelect);
	return objStyle;
}

function getTypeLabel(chk){
	var style=getStyleFromCookie();
	if(chk.checked){
		if(style==null){
			base.removeChildDiv(divSelectLabel);
			$.get(sPortletJSP+"label_setup.jsp",function(data){
				$(divSelectLabel).html(data);
			});
			divSelectLabel.style.display="block";
		}else
			showLabelFeature();
	}
	else{
		if(style!=null)
			hiddenLabel();
		else
			divSelectLabel.style.display="none";
	} 
		
}
/**
 *  		hidden label
 */
function hiddenLabel() {
	/** value selected in select box layer */
	var indexLayer = 0;
	if(selectToolSelectLayer==null){
		for(var n=0;n<vectorArray.length;n++){
			if(vectorArray[n].LayerId==layerParam){
				indexLayer=vectorArray[n].IndexLayer;
				break;
			}
		}
	}else
		indexLayer = selectToolSelectLayer[selectToolSelectLayer.selectedIndex].value;
	/** layer selected */
	var layer = map.layers[indexLayer];
	/** create rule */
	var ruleHidden = new OpenLayers.Rule({
		symbolizer : {
			label : '',
		}
	});
	/** add new rule in style of vector */
	layer.styleMap.styles["default"].addRules([ ruleHidden ]);
	/** redraw vector */
	layer.redraw();
	map.layers[indexLayer].setVisibility(false);
}
function setUpLabel(chkAttr,selectFont,selectSize,inputColor,chkBold,chkItalic,rdoAddr){
	var i=0;
	/** cookie name */
	var cookieName=null;
	/** cookie value */
	var cookieValues = null;
	/** string label */
	var strlabel = '';
	/** label align */
	var labelAlign = null;
	/** text bold */
	var textBold = null;
	/** text italic */
	var textItalic = null;
	/** font family name selected in select box */
	var fontFamilyValue = null;
	/** font size value selected in select box */
	var fontSizeValue = null;
	var textColor=inputColor.value;
	/** index layer current */
	var indexLayer=null;
	if(selectToolSelectLayer==null){
		for(i=0;i<vectorArray.length;i++){
			if(vectorArray[i].LayerId==layerParam){
				indexLayer=vectorArray[i].IndexLayer;
				cookieName=vectorArray[i].LayerId;
				break;
			}
		}
	}else{
		indexLayer=selectToolSelectLayer[selectToolSelectLayer.selectedIndex].value;
		cookieName=vectorArray[indexLayer-numBaseLayer].LayerId;
	}
	for (i = 0; i < chkAttr.length; i++) {
		if (chkAttr[i].checked) {
			strlabel += chkAttr[i].title + ": " + '$' + "{" + chkAttr[i].value + "} ";
		}
	}
	/** check label null or not null */
	if(strlabel==''){
		var c=confirm(LANGUAGE["labelNull"]+"\n"+LANGUAGE["end"]);
		if(!c)
			return;
	}
	/** check these checkbox checked and set value for font style */
	if(chkBold.checked)
		textBold=chkBold.value;
	if(chkItalic.checked)
		textItalic=chkItalic.value;
	fontFamilyValue=selectFont[selectFont.selectedIndex].value;
	fontSizeValue=selectSize[selectSize.selectedIndex].value;
	for(i=0;i<rdoAddr.length;i++){
		if(rdoAddr[i].checked){
			labelAlign=rdoAddr[i].value;
			break;
		}
	}
	if(textColor==null || textColor == '')
		textColor="#000000";
	cookieValues=strlabel + "," + textColor + "," + fontFamilyValue + ","
	+ textBold + "," + textItalic + "," + fontSizeValue + ","+ labelAlign;
	/** create cookie */
	Cookies.setCookie(cookieName, cookieValues, 365);
	showLabelFeature();
	divSelectLabel.style.display="none";
}

function showLabelFeature(){
	/** value selected in select box layer */
	var sValue = null;
	if(selectToolSelectLayer==null){
		for(var i=0;i<vectorArray.length;i++){
			if(vectorArray[i].LayerId==layerParam){
				sValue=vectorArray[i].IndexLayer;
				break;
			}
		}
	}else
		sValue=selectToolSelectLayer[selectToolSelectLayer.selectedIndex].value;
	/** string label */
	var label = '';
	/** label value */
	var labelValue = '';
	/** layer */
	var layer = null;
	/** id of layer */
	var layerId = null;
	/** value of style */
	var styleValue = null;
	/** array value */
	var arrValue = null;
	/** array label */
	var arrLabel = null;
	/** layer current */
	layer = map.layers[sValue];
	/** id of layer current */
	layerId = vectorArray[sValue - numBaseLayer].LayerId;
	/** style of feature */
	styleValue = Cookies.getCookie(layerId);
	/** if style null show notice */
	arrValue = styleValue.split(",");
	label = arrValue[0];
	label.substring(0, (label.length - 1));
	arrLabel = label.split("} ");
	for ( var i = 0; i < arrLabel.length; i++) {
		labelValue += arrLabel[i] + "}\n";
	}
	labelValue = labelValue.substring(0, (labelValue.length - 2));
	/** create rule */
	var xoffset=null;
	var yoffset=null;
	if(arrValue[6]=="cm"){
		xoffset = new OpenLayers.Pixel(20,0);
		yoffset = new OpenLayers.Pixel(20,0);
	}else{
		xoffset = new OpenLayers.Pixel(20,0);
		yoffset = new OpenLayers.Pixel(20,0);
	}
	var newRule = new OpenLayers.Rule({
		symbolizer : {
			label : labelValue,
			fontColor : arrValue[1],
			fontFamily : arrValue[2],
			fontWeight : arrValue[3],
			fontStyle : arrValue[4],
			fontSize : arrValue[5],
			labelAlign : arrValue[6],
			labelXOffset:  xoffset,
			labelYOffset:  yoffset,
		}
	});
	/** layer add new rule */
	layer.styleMap.styles["default"].addRules([ newRule ]);
	/** redraw layer */
	layer.redraw(true);
	map.layers[sValue].setVisibility(true);
}

function closeSetupLabel(){
	divSelectLabel.style.display="none";
	var objChk=document.getElementById(nameSpace+"_show_hidden_label");
	objChk.checked=false;
}

function formSelectTooltip(){
	var tblAttr=document.createElement("table");
	var divRoot = document.createElement("div");
	var divHpcs = document.createElement("div");
	var fieldSet = document.createElement("fieldset");
	divRoot.setAttribute("class", "para para-none-scroll");
	divHpcs.setAttribute("class", "hpcs pt10");
	fieldSet.setAttribute("class", "form-mapleft-menu");
	for ( var i = 0; i < arrFieldAction.length; i++) {
		if(arrFieldAction[i].IsShowLabel){
			var divInput=document.createElement("div");
			divInput.setAttribute("class", "input input-mdf wd-s1");
			var tr = document.createElement("tr");
			var tdAlias = document.createElement("td");
			var tdInput = document.createElement("td");
			var label = document.createElement("label");
			label.innerHTML = arrFieldAction[i].Alias;
			var objChk = document.createElement("input");
			objChk.type = "radio";
			objChk.name = "rdo_list";
			objChk.value = arrFieldAction[i].Name;
			objChk.title = arrFieldAction[i].Alias;
			objChk.style.width = "40px";
			divInput.appendChild(objChk);
			divInput.appendChild(label);
			tdInput.appendChild(divInput);
			tdAlias.appendChild(divInput);
			tr.appendChild(tdInput);
			tr.appendChild(tdAlias);
			tblAttr.appendChild(tr);
		}
	}
	var divSubmit_ok=document.createElement("div");
	divSubmit_ok.setAttribute("class","submit submit-mdf");
	var inputAccept=document.createElement("input");
	var inputCancel=document.createElement("input");
	var objLabel=document.createElement("label");
	objLabel.innerHTML="&nbsp;&nbsp;";
	inputAccept.type="submit";
	inputAccept.value=LANGUAGE["btn_Map_AddFeature"];
	inputAccept.setAttribute("class","bt-search-ln");
	inputAccept.onclick=setUpTooltip;
	inputCancel.type="submit";
	inputCancel.value="Bỏ qua";
	inputCancel.setAttribute("class","bt-cancel-ln");
	inputCancel.onclick=closeSelectTooltip;
	var objTR_Click = document.createElement("tr");
	var objTD_Click_Ok= document.createElement("td");
	objTD_Click_Ok.colSpan="2";
	divSubmit_ok.appendChild(inputAccept);
	divSubmit_ok.appendChild(objLabel);
	divSubmit_ok.appendChild(inputCancel);
	objTD_Click_Ok.appendChild(divSubmit_ok);
	objTR_Click.appendChild(objTD_Click_Ok);
	tblAttr.appendChild(objTR_Click);
	fieldSet.appendChild(tblAttr);
	divHpcs.appendChild(fieldSet);
	divRoot.appendChild(divHpcs);
	base.removeChildDiv(divSelectTooltip);
	divSelectTooltip.appendChild(divRoot);
	$(".form-mapleft-menu select").uniform();
	jQuery("input[type=checkbox].checkbox-unf").uniform();
}

function closeSelectTooltip(){
	divSelectTooltip.style.display="none";
	var objTooltip=document.getElementById(nameSpace+"_show_hidden_tooltip");
	objTooltip.checked=false;
}

function setUpTooltip(){
	var rdoList=document.getElementsByName("rdo_list");
	for(var i=0;i<rdoList.length;i++){
		if(rdoList[i].checked){
			fieldAlias=rdoList[i].title;
			fieldName=rdoList[i].value;
			break;
		}
	}
	var sValue = null;
	if(selectToolSelectLayer==null){
		for(var i=0;i<vectorArray.length;i++){
			if(vectorArray[i].LayerId==layerParam){
				sValue=vectorArray[i].IndexLayer;
				break;
			}
		}
	}else
		sValue=selectToolSelectLayer[selectToolSelectLayer.selectedIndex].value;
	map.layers[sValue].setVisibility(true);
	setTooltipFeature(layerActivate);
	divSelectTooltip.style.display="none";
}

function showSetupLabel(){
	base.removeChildDiv(divSelectLabel);
	$.get(sPortletJSP+"label_setup.jsp",function(data){
		$(divSelectLabel).html(data);
	});
	divSelectLabel.style.display="block";
}
